package shop

import (
	"context"
	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"
	"fast-boot/app/model"
	"fast-boot/common/globalkey"
	"github.com/hwUltra/fb-tools/gormx"
	"github.com/jinzhu/copier"

	"github.com/zeromicro/go-zero/core/logx"
)

type PageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 店铺列表
func NewPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PageLogic {
	return &PageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PageLogic) Page(req *types.ShopPageReq) (resp *types.ShopPageResp, err error) {
	m := model.PmsShopModel{}

	total := int64(0)
	if err := l.svcCtx.GormClient.GormDb.Model(m).
		Scopes(
			m.WithStatus(req.Status),
			m.WithKeywords(req.Keywords)).
		Count(&total).Error; err != nil {
		return nil, err
	}

	items := make([]*model.PmsShopModel, 0)
	if total > 0 {
		l.svcCtx.GormClient.GormDb.Model(m).
			Scopes(
				gormx.Paginate(int(req.PageNum), int(req.PageSize)),
				m.WithStatus(req.Status),
				m.WithKeywords(req.Keywords)).
			Order("id asc").Find(&items)

	}
	list := make([]*types.Shop, 0)
	if err := copier.CopyWithOption(&list, items, globalkey.CopierProtoOptions); err != nil {
		return nil, err
	}

	return &types.ShopPageResp{List: list, Total: total}, nil
}
